package com.ty.web;

import com.ty.bean.Student;
import com.ty.service.StudentService;
import com.ty.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 田野
 */
@WebServlet(urlPatterns = "/Educational/student/addStu")
public class AddStuServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*// 首先需要处理一下乱码的问题
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");*/
        // 1、接收参数 比较多
        String stuNo = req.getParameter("stuNo");
        String stuName = req.getParameter("stuName");
        String gid = req.getParameter("gid");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idNumber = req.getParameter("idNumber");
        String profession = req.getParameter("profession");
        String introduction = req.getParameter("introduction");
        // 2、调用service层方法
        StudentService service = new StudentServiceImpl();
        // 将属性封装成Student对象
        Student student = new Student();
        student.setStuNo(stuNo);
        student.setStuName(stuName);
        student.setGid(Integer.parseInt(gid));
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setPhone(phone);
        student.setRegistered(registered);
        student.setAddress(address);
        student.setPolitics(politics);
        student.setIdNumber(idNumber);
        student.setProfession(profession);
        student.setIntroduction(introduction);

        int i = service.insertStu(student);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i>0){
            // 3、跳转页面到list.jsp
            /*writer.print("<script>alert('新增成功！！！！')；location.href='list.jsp'</script>");*/
            // 前端发送请求的方式，之前都是通过超链接或者是表单，而这里是通过js来发送请求，
            // 之后再跳转到list.jsp 因为在list.jsp 有 <c:forEach items="${stulist}" var="stu"> 而此时 stulist并没有存值，
            // 因此会报错，所以不能直接使用上面的代码中的直接跳转到list.jsp页面。
            writer.print("<script>alert('新增成功！！！！');location.href='/Educational/student/getStudentList'</script>");

        }else {
            // 同样的道理，这里也不能直接跳转到add.jsp页面，因为有 <c:forEach items="${glist}" var="g">
            // 而这个 glist 并没有存值
            /*writer.print("<script>alert('新增失败！！！！')；location.href='add.jsp'</script>");*/
            writer.print("<script>alert('新增失败！！！！');location.href='/Educational/student/getGradeList'</script>");
        }

        /**
         * 也就是无论是新增成还是失败，都会给后台发送一个请求
         * 处理这个请求之后再跳转到相应的页面
         */

    }
}
